import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class Solution {
    StringBuffer stringbuffer;
    List<String> ret;
    Map<Character, String> map;
    public List<String> letterCombinations(String digits) {
        stringbuffer = new StringBuffer();
        ret = new ArrayList<>();
        map = new HashMap<>();
        if(digits.length() == 0)  return ret;
        map.put('2', "abc");
        map.put('3', "def");
        map.put('4', "ghf");
        map.put('5', "jkl");
        map.put('6', "mno");
        map.put('7', "pqrs");
        map.put('8', "tuv");
        map.put('9', "wxyz");
        dfs(digits, 0);
        return ret;
    }
    private void dfs(String digits, int index) {
        if(index == digits.length()) {
            ret.add(stringbuffer.toString());
            return;
        }
        char ch = digits.charAt(index);
        String str = map.get(ch);
        for(int i = 0; i < digits.length(); i++) {
            stringbuffer.append(String.valueOf(str.charAt(i)));
            dfs(digits, index+1);
            stringbuffer.deleteCharAt(stringbuffer.length()-1);
        }
    }

    public int maxTurbulenceSize(int[] arr) {
        int n = arr.length;
        int[] dp = new int[n];
        dp[0] = 1;
        if(n == 1) {
            return dp[0];
        }
        if(arr[0] != arr[1]) {
            dp[1] = 2;
        }else {
            dp[1] = 1;
        }
        int ret = dp[1];
        for(int i = 2; i < n; i++) {
            if((arr[i-1] > arr[i] && arr[i-2] < arr[i-1]) || arr[i-1] < arr[i] && arr[i-2] > arr[i-1]) {
                dp[i] = dp[i-1] + 1;
            }else if(arr[i-1] != arr[i]){
                dp[i] = 2;
            }else {
                dp[i] = 1;
            }
            ret = Math.max(ret, dp[i]);
        }
        return ret;
    }
}